package com.laizhenghua.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laizhenghua.demo.domain.po.UserEntity;
import com.laizhenghua.demo.mapper.UserMapper;
import com.laizhenghua.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * TODO
 *
 * @Description
 * @Author laizhenghua
 * @Date 2023/7/2 17:00
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserEntity getUserByName(String username) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return this.getOne(wrapper);
    }

    @Override
    public UserEntity saveUser(UserEntity entity) {
        if (null == entity.getCreateTime()) {
            entity.setCreateTime(new Date());
        }
        entity.setPassword(passwordEncoder.encode(entity.getPassword()));
        UserEntity user = this.getUserByName(entity.getUsername());
        if (null == user) {
            this.save(entity);
            user = entity;
        } else {
            Long id = user.getId();
            BeanUtils.copyProperties(entity, user);
            user.setId(id);
            this.updateById(user);
        }
        return user;
    }
}
